<!DOCTYPE html>
<html lang="en">
<head>
    {% load static %}
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="{% static './layui/css/layui.css' %}" rel="stylesheet">
    <script src="{% static './layui/layui.js' %}"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<form class="layui-form" action="">
  <div class="layui-form-item">
    <label class="layui-form-label"><span style="color: red">*</span>岗位名称</label>
    <div class="layui-input-block">
      <input type="text" name="post_name" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">职责描述</label>
    <div class="layui-input-block">
      <textarea placeholder="" class="layui-textarea" id="description" name="description"></textarea>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
<script>
layui.use(['form'], function(){
  var form = layui.form
  ,layer = layui.layer;

  //监听提交
  form.on('submit(demo1)', function(data){
      var post_name = data.field['post_name']
      var description = data.field['description']
      console.log(post_name)
      console.log(description)
      $.ajax({
        method: "POST",
        url: "{%url 'xyss_admin:postAddApi' %}",
        data: { post_name: post_name, description: description},
        success: function (res) {
            if(res['code'] == 0){
                layer.msg(res['msg'],{time:2000},
                    function(){
                        parent.layer.closeAll()
                        parent.location.reload();
                    }
                );
            }else{
                layer.msg(res['msg']);
            }
        },
        error: function(res) {
            console.log("失败了: " + res['msg']);
        }
      });
      return false;
  });
});
</script>
</body>
</html>